package com.plugin.uniapp.dialog;

import android.animation.ObjectAnimator;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.plugin.uniapp.R;
import com.plugin.uniapp.Task;

import java.util.ArrayList;
import java.util.List;

public class TasksView extends BaseView {
    public class TaskUI {
        public ImageView view;
        public ObjectAnimator anim;
        public boolean isDoing;

        public TaskUI(ImageView view) {
            this.view = view;
        }

        public void setWaiting() {
            isDoing = false;
            if (anim != null) {
                anim.cancel();
                anim = null;
            }
            view.setVisibility(View.INVISIBLE);
        }

        public void setFinish() {
            isDoing = false;
            if (anim != null) {
                anim.cancel();
                anim = null;
            }
            view.setVisibility(View.VISIBLE);
            view.setImageResource(R.drawable.finished);
            view.setRotation(0f); // 重置旋转角度
        }

        public void setDoing() {
            isDoing = true;
            view.setVisibility(View.VISIBLE);
            view.setImageResource(R.drawable.doing);
            if (anim == null) {
                anim = ObjectAnimator.ofFloat(view, "rotation", 0f, 350f);
                anim.setDuration(1000);
                anim.setRepeatCount(ObjectAnimator.INFINITE);
                anim.setRepeatMode(ObjectAnimator.RESTART);
                anim.setInterpolator(new LinearInterpolator());
            }
            anim.start();
        }
    }

    public static Handler handler = new Handler(Looper.getMainLooper());
    private final ScrollView scrollView;
    private final LinearLayout scrollContainer;
    private final List<TaskUI> viewList = new ArrayList<>();

    public TasksView(MultiViewDialog dialog) {
        super(dialog, R.layout.view_tasks);
        scrollView = layout.findViewById(R.id.scroll_view);
        scrollContainer = layout.findViewById(R.id.scroll_container);
    }

    public void createItems(List<Task> taskList) {
        scrollContainer.removeAllViews();
        viewList.clear();
        View itemView;
        for (int i = 0; i < taskList.size(); i++) {
            Task task = taskList.get(i);
            if (i < scrollContainer.getChildCount()) {
                itemView = scrollContainer.getChildAt(i);
            } else {
                itemView = inflater.inflate(R.layout.view_tasks_item, scrollContainer, false);
                scrollContainer.addView(itemView); // 先添加 View，再绑定数据
            }
            ImageView imageView = itemView.findViewById(R.id.item_image);
            TextView textView = itemView.findViewById(R.id.item_text);
            TextView timeTextView = itemView.findViewById(R.id.item_time_text);
            textView.setText(task.name);
            timeTextView.setText(" (" + task.time + ")");
            viewList.add(new TaskUI(imageView));
        }
        refreshUpdate();
    }

    public void refreshUpdate() {
        if (isVisible) {
            if (!dialog.canListenStatusPush && !dialog.isWaitingStart && !dialog.isWaitingLogin) {
                dialog.showLoading();
            } else {
                dialog.hideLoading();
            }
            for (int i = 0; i < viewList.size(); i++) {
                TaskUI task = viewList.get(i);
                task.isDoing = false;
                if (!dialog.canListenStatusPush) {
                    task.setWaiting();
                } else {
                    if (dialog.currTaskIndex >= 0) {
                        if (i < dialog.currTaskIndex) {
                            task.setFinish();
                        } else if (i == dialog.currTaskIndex) {
                            task.view.setVisibility(View.VISIBLE);
                            if (dialog.currTaskComplated) {
                                task.setFinish();
                            } else {
                                task.setDoing();
                            }
                        } else {
                            task.view.setVisibility(View.INVISIBLE);
                            task.setWaiting();
                        }
                    } else {
                        task.view.setVisibility(View.INVISIBLE);
                        task.setWaiting();
                    }
                }
            }
            if (dialog.canListenStatusPush) {
                scrollToCenter(dialog.currTaskIndex, 100);
            }
        }
    }

    private void scrollToCenter(int targetIndex, int delay) {
        handler.postDelayed(() -> {
            scrollView.post(() -> {
                View targetView = scrollContainer.getChildAt(targetIndex);
                if (targetView != null) {
                    int scrollViewHeight = scrollView.getHeight();
                    int targetY = targetView.getTop();
                    int targetHeight = targetView.getHeight();
                    int scrollToY = targetY - (scrollViewHeight / 2) + (targetHeight / 2);
                    scrollView.smoothScrollTo(0, scrollToY);
                }
            });
        }, delay);
    }

    public void onTaskUpdate() {
        if (isVisible) {
            refreshUpdate();
        }
    }

    public void insertExtraTask(Task extraTask) {
        View itemView = inflater.inflate(R.layout.view_tasks_item, scrollContainer, false);
        scrollContainer.addView(itemView);
        ImageView imageView = itemView.findViewById(R.id.item_image);
        TextView textView = itemView.findViewById(R.id.item_text);
        TextView timeTextView = itemView.findViewById(R.id.item_time_text);
        textView.setText(extraTask.name);
        timeTextView.setText(" (" + extraTask.time + ")");
        viewList.add(extraTask.index, new TaskUI(imageView));
    }

    @Override
    public void hide() {
        super.hide();
        for (int i = 0; i < viewList.size(); i++) {
            viewList.get(i).setWaiting();
        }
    }
}
